package com.yowasa.xiuxian.engine.message.hone;

import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.engine.HoneEngine;
import com.yowasa.xiuxian.engine.hone.MentalInterface;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.StageEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.service.helper.UserTagHelper;
import com.yowasa.xiuxian.util.DateXUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.MessageFormat;
import java.util.Date;

@CMD(name = "/结束修炼", scope = CmdScopeEnum.ALL)
public class EndGetExpHandler extends CmdMessageHandler {
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private UserTagHelper userTagHelper;

    @Override
    public void execute(SendMessage message) {
        UserBO user = userHelper.checkOpUser(message);
        if (!user.getUserConfig().isXiuLianFlag()) {
            throw new HandlerException("未曾开始修炼");
        }
        MentalInterface mental = HoneEngine.mentalMap.get(user.getUserConfig().getMental());
        int days = DateXUtil.getFrom(user.getUserConfig().getGetExpStartTime());
        int exp = mental.getExp(user, days);
        int out = userHelper.addExp(user, exp);
        if (out >= 20&&user.getUserConfig().getStudiedSpecialEffect().contains("百锻成仙") && user.getStage() <9 && user.getStage() > 3) {
            int value = userTagHelper.getValue(user.getUserId(), XiuxianConstants.BAIDUAN_TIMES);
            if(value >= 6){
                messageHelper.replyMys(message, "百锻次数已达上限");
            }else {
                userTagHelper.addOne(user.getUserId(), XiuxianConstants.BAIDUAN_TIMES);
            }
            messageHelper.replyMys(message, "由于百段成仙你的战斗力提高了");
        }
        String tuPoMsg = "";
        if (user.getLevel() == 100) {
            tuPoMsg = ",随时可以突破";
        }
        user.setLockEndTime(new Date());
        user.getUserConfig().setXiuLianFlag(false);
        userHelper.saveUserInfo(user);
        messageHelper.replyMys(message, MessageFormat.format("结束修炼,本次修炼时长{0}天,你感觉你现在处于{1}{2}{3}", days, StageEnum.getById(user.getStage()).getName(), getDesc(user.getLevel()), tuPoMsg));
    }

    private String getDesc(int expect) {
        if (expect < 30) {
            return "初期";
        } else if (expect < 60) {
            return "中期";
        } else if (expect < 100) {
            return "后期";
        }
        return "圆满";
    }

    public String desc(){
        return new StringBuffer().append("停止修炼").toString();

    }

}
